package com.woniuxy.personal.core.service.impl;

import com.woniuxy.common.CommonResult;
import com.woniuxy.personal.client.HomepageFeignClient;
import com.woniuxy.personal.client.dto.CarMycar;
import com.woniuxy.personal.core.model.PlUser;
import com.woniuxy.personal.core.mapper.PlUserMapper;
import com.woniuxy.personal.core.service.PlUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.personal.core.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author gmx
 * @since 2021-06-29
 */
@Service
public class PlUserServiceImpl extends ServiceImpl<PlUserMapper, PlUser> implements PlUserService {

    @Resource
    PlUserMapper plUserMapper;
    @Resource
    HomepageFeignClient homepageFeignClient;

    @Override
    public Integer selectScore(String username) {
        return plUserMapper.selectScore(username);
    }

    @Override
    public PlUser selectUser(String username) {
        return plUserMapper.selectUser(username);
    }

    @Override
    public BigDecimal appraisal(Integer userId, Integer carId) {
        CommonResult<List<CarMycar>> commonResult = homepageFeignClient.selecMyCar(userId);
        List<CarMycar> carMycars = commonResult.getData();
        BigDecimal price = null;
        for (CarMycar mycar : carMycars
        ) {
            if (mycar.getId().equals(carId)) {
                price = mycar.getPrice();
                Date viTime = mycar.getViTime();
                Double mile = mycar.getMile();
                Date now = new Date();
                //计算购车时间
                long l = now.getTime() - viTime.getTime();
                //将时间差转换为年
                long year = l / 1000 / 60 / 60 / 24 / 30 / 12;
                if (year < 1) {
                    price = price.multiply(BigDecimal.valueOf(0.9));
                } else if (year > 1 && year < 3) {
                    price = price.multiply(BigDecimal.valueOf(0.8));
                } else if (year > 3) {
                    price = price.multiply(BigDecimal.valueOf(0.7));
                }
                if (mile < 10000) {
                    price = price.multiply(BigDecimal.valueOf(0.9));
                } else if (mile > 10000 && mile < 30000) {
                    price = price.multiply(BigDecimal.valueOf(0.8));
                } else if (mile > 30000) {
                    price = price.multiply(BigDecimal.valueOf(0.7));
                }
                if (mycar.getIsaccident() == 1) {
                    price = price.multiply(BigDecimal.valueOf(0.5));
                }
            }
        }

        return price;
    }

    @Override
    public List<CarMycar> selectMycar(Integer userId) {
        return homepageFeignClient.selecMyCar(userId).getData();
    }

    @Override
    public int insertUser(UserVo userVo) {
        PlUser user = new PlUser();
        user.setUsername(userVo.getPhone());
        user.setPassword(userVo.getPassword());
        int insert = this.plUserMapper.insert(user);
        return insert;
    }

}
